<ui:composition xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:f="http://java.sun.com/jsf/core"
                xmlns:c="http://java.sun.com/jsp/jstl/core"
                xmlns:fn="http://java.sun.com/jsp/jstl/functions"
                xmlns:ds="http://deltaset.org/deltaset-faces"
                xmlns:mi="http://java.sun.com/jsf/composite/deltaset.meta/internal"
                xmlns:composite="http://java.sun.com/jsf/composite"> 
    <h:commandButton value="REFRESHER" id="refreshPager" style="display:none">
        <f:ajax render="#{pagerId}"/>
    </h:commandButton>
    <ds:pagedTable pageHelper="#{meta.all(param.page)}"
                   pageParamName="page"
                   showPager="both"
                   cols="#{meta.listFields.size()+1}"
                   openRowButtonId="openDetailRow">
        <f:facet name="header">
            <c:forEach items="#{meta.listFields}" var="field">
                <th>#{meta.labelField(field, meta.modelClass)}</th>
            </c:forEach>                   
            <th>Id</th>
        </f:facet>
        <f:facet name="row"> 
            <mi:list-fields fields="#{meta.listFields}" model="#{item}" meta="#{meta}"/>
        </f:facet>

        <h:commandButton id="openDetailRow" value="openDetailRow" style="display:none">
            <f:ajax execute="@form" render="detail-row" />
            <f:param name="recordId" value="#{meta.getModelId(item)}"/>
        </h:commandButton>                               

        <h:panelGroup id="detail-row" style="display:block" >                
            <c:set var="detail" value="#{meta.model}"/>
            <h:panelGroup rendered="#{meta.isNew(detail)}" style="text-align: center" layout="block">
                <b>Loading...</b>
            </h:panelGroup>
            <h:panelGroup rendered="#{not meta.isNew(detail)}">
                <c:forEach items="#{meta.contextActions}" var="action">
                    <mi:commandButton metaAction="#{action}"
                                      model="#{detail}"
                                      meta="#{meta}"
                                      noVoidExecute="@this" 
                                      noVoidOnEvent="#{editPanelOpen}"
                                      noVoidRender="#{editPanelFormId}"
                                      voidExecute="@form"
                                      voidOnEvent="function(data){if('success'==data.status){ document.getElementById('#{pagerId}:refreshPager'.substring(1)).onclick(); }}"                                      
                                      parameterExecute="@form"
                                      parameterOnEvent="#{executePanelOpen}"
                                      parameterRender="#{executorId}"
                                      />                   
                </c:forEach>               

                <div>
                    <c:forEach items="#{meta.otherContextActions}" var="action">                        
                        <mi:commandButton metaAction="#{action}"
                                          model="#{detail}"
                                          meta="#{meta}"
                                          noVoidExecute="@form"
                                          noVoidOnEvent="#{executePanelOpen}"
                                          noVoidRender="#{executorId}"
                                          voidExecute="@form"
                                          voidOnEvent="#{executePanelOpen}"
                                          voidRender="#{executorId}"
                                          parameterExecute="@form"
                                          parameterOnEvent="#{executePanelOpen}"
                                          parameterRender="#{executorId}"
                                          style="font-size: 0.8em;"
                                          />
                    </c:forEach>
                </div>
                <ds:fieldset>                    
                    <mi:detail-fields fields="#{meta.detailFields}" model="#{detail}" meta="#{meta}" owner="#{meta.modelClass}"/>
                </ds:fieldset>
            </h:panelGroup>
        </h:panelGroup>
    </ds:pagedTable>

</ui:composition>
